﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace DataAccess
{
    public class BatLoi
    {
        public static bool ThongBao(string Text, string title)
        {
            MessageBox.Show(Text, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
            return false;
        }
        public static bool ThongBao(string Text)
        {
            if (Text.Trim() != "")
                MessageBox.Show(Text, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return false;
        }

        public static bool DgResult(string Text)
        {
            return DialogResult.OK == MessageBox.Show(Text, "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
        }

        public static bool NhapGia(object sender, KeyPressEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (!NhapSo(sender, e))
                return false;
            else
                if (!tb.Text.Contains('.') && tb.Text != "" && e.KeyChar == '.')
                    return false;
            return true;
        }
        public static bool NhapSo(object sender, KeyPressEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if ((e.KeyChar >= '0'
                && e.KeyChar <= '9')
                || e.KeyChar == 8)
                return false;
            return true;
        }
        public static bool NhapCMND(object sender, KeyPressEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb.Text.Length < 9 && !NhapSo(sender, e))
                return false;
            return !(e.KeyChar == 8);
        }
        public static bool NhapSDT(object sender, KeyPressEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb.Text.Length < 11 && !NhapSo(sender, e))
                return false;
            return !(e.KeyChar == 8);
        }

        public static bool NhapTen(object sender, KeyPressEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if ((e.KeyChar > 'a' && e.KeyChar <= 'z') || (e.KeyChar > 'A' && e.KeyChar <= 'Z'))
                return false;
            return !(e.KeyChar == 8);
        }

        public static bool TextBoxEmpty(TextBox tb)
        {
            if (tb.Text.Trim() != "")
                return true;
            tb.Select();
            return ThongBao("Vui lòng kiểm tra lại các giá trị");
        }

        public static bool ErrorNumberInt(TextBox tb, string Message)
        {
            try
            {
                int t = int.Parse(tb.Text);
                return true;
            }
            catch (Exception)
            {
                return ThongBao(Message);
            }
        }
        public static bool ErrorNumberFloat(TextBox tb, string Message)
        {
            try
            {
                float t = float.Parse(tb.Text);
                return true;
            }
            catch (Exception)
            {
                return ThongBao(Message);
            }
        }

        public static bool CMND(TextBox tb, string Message)
        {
            string Text = tb.Text;
            string Patern = @"^\d{2}$";
            Regex rg = new Regex(Patern);
            if (!rg.IsMatch(Text))
            {
                tb.Select();
                return ThongBao(Message);
            }
            return true;
        }
        public static bool SDT(TextBox tb, string Message)
        {
            string Text = tb.Text;
            if (Text.Trim() == "")
                return true;
            string Patern = @"^\d{10}|\d{11}$";
            Regex rg = new Regex(Patern);
            if (!rg.IsMatch(Text))
            {
                tb.Select();
                return ThongBao(Message);
            }
            return true;
        }

        public static bool TextBoxEmpty(TextBox tb, string Message)
        {
            if (tb.Text.Trim() != "")
                return true;
            tb.Select();
            return ThongBao(Message);
        }



    }
}
